<?php

include('inc/init.php');

$smarty->template_dir = 'templates';

include 'inc/directconn.php';
$partnerid = $_POST['partneridpost'];

//get company data
$sql = "select email from ats_company";
$result = @mysql_query($sql);
$row = mysql_fetch_row($result);
$atsmail = $row[0];

//get partner data
$sql = "select id,email,partnername from ats_partners where id='$partnerid'";
$result = @mysql_query($sql);
$row = mysql_fetch_row($result);

$rescount = mysql_num_rows($result);

$name = $_POST['txtname'];
$email = $_POST['txtmail'];
$domisili = $_POST['txtdomisili'];
$subject = $_POST['txtsubject'];
$message = $_POST['txtmsg'];
$partnermail = $row[1];
$partnername = $row[2];
if (isset($_POST['partnernamepost']))
    $partnermail = $_POST['partnernamepost'];

$sql = "insert into ats_partner_contact (name,email,domisili,subject,message,partnermail, partnerid) values('" . $name . "','" . $email . "','" . $domisili . "','" . $subject . "','" . $message . "','" . $partnermail . "','" . $partnerid . "')";
$res = $db->prepare($sql);

$res->execute();

//send to partner mail, set in config.php
if ($sendtopartnermail == 1) {
    $to = $partnermail . ', ';
    $to .= $atsmail;

    // subject
    $subject = 'Inquiries from ATS Tour';

    // message
    $message = "
    <html>
    <head>
        <title>Inquiries from ATS Tour</title>
    </head>
    <body>
        <p>Dear $partnername,</p>
        <br />
        <p>Here is an inquiries from our customer named $name (<a href='mailto:$email'>$email</a>)</p>
        <br />
        <br />
        <p>$message</p>
        <br />
        <br />
        <br />
        <p>Regards,</p>
        <p>ATS Tour</p>
    </body>
    </html>
    ";

    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= "To: $partnername <$partnermail>" . "\r\n";
    $headers .= "From: $name <$email>" . "\r\n";
    $headers .= "Cc: $atsmail" . "\r\n";
    //$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

    // Mail it
    mail($to, $subject, $message, $headers);
}


echo "Thank you, your inquiries have been saved";
?>
